extends Node
@onready var start = $ColorRect/start
@onready var close = $ColorRect/close
@onready var player_save :Array #——————>>>用来承载登陆服务器发送的玩家数据
@onready var game_ser :StreamPeerTCP = StreamPeerTCP.new()

#因为没有数据库，故而将游戏服务与登陆服务器通过TCP连接起来，如此好操作玩家的存档数据，你也可以
#将游戏服务器与登陆服务器合为一个项目，作为后续拓展项目，我这里还是分开设计的。

func  _ready():
	start.disabled = false
	close.disabled = true



func _process(delta):     #此脚本的进程程序将不断检索TCP的连接数据，以此来查收登陆服务器发送过来的玩家存档字典
	if game_ser.get_status() == 2:
		if game_ser.get_available_bytes() > 2:
			var data = game_ser.get_var()
			$gamemess.append_text(str(data) + "\n")
			if typeof(data) == TYPE_ARRAY:
				print(data)
			else:
				var code = data.get_slice(",",0)
				if code == "x":
					$gamemess.append_text("与登陆服务器的链接已断开...\n")
					_on_messbox_ok_pressed()         
					



func _on_start_game_pressed():
	game_ser.connect_to_host("127.0.0.1",9527)
	game_ser.poll()
	if game_ser.get_status() == 2: #————————>>游戏服务器一但连接到登陆服务器就发送信息让登陆服务将此链接保存
		game_ser.put_var("g,游戏服务器已连接...")
		start.disabled = true
		close.disabled = false
		$game.start_game()
	else :
		$gamemess.append_text("登陆服务器未连接...\n")
		game_ser.disconnect_from_host()
	


func _on_close_game_pressed():
	$mess.visible = true
	
	
func _on_messbox_ok_pressed():
	start.disabled = false
	close.disabled = true
	$mess.visible = false
	game_ser.disconnect_from_host()  #关掉与登陆服务器的TCP链接
	$game.close_game() #———————————————>>>>关掉对等体peer链接  


func _on_messbox_no_pressed():
	$mess.visible = false

@rpc("any_peer" , "call_local")
func _new_text(new_text:String):
	pass
